package evolution.cli;

import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.Properties;
import java.util.TreeMap;

import evolution.ResourceFactory;
import evolution.UUIDGenerator;
import evolution.VersionedDirectoryTreeBuilder;

public class Main
{
	static Map<String, Command> commands = new TreeMap<String, Command>();

	public static void main(String[] args) throws Exception
	{
		ResourceFactory resourceFactory = new FileResourceFactory();
		UUIDGenerator uuidGenerator = new ProductionUUIDGenerator();
		VersionedDirectoryTreeBuilder builder = new VersionedDirectoryTreeBuilder(resourceFactory, uuidGenerator);

		commands.put("init", new RepositoryInitializationCommand(builder));
		commands.put("wipe", new WipeRepositoryCommand(builder));
		commands.put("status", new StatusCommand(builder));

		if (args.length == 0)
		{
			help();
		}
		else
		{
			Command command = commands.get(args[0]);
			if (command == null)
			{
				System.err.println("No such command: " + args[0]);
				help();
			}
			else
			{
				command.execute(args);
			}
		}
	}

	private static void help() throws IOException
	{
		InputStream in = Main.class.getResourceAsStream("/project.properties");
		if (in != null)
		{
			Properties p = new Properties();
			p.load(in);
			System.out.println(p.getProperty("project.name") + " version " + p.getProperty("project.version"));
		}
		System.out.println("Use the following commands:");
		for (String cmd : commands.keySet())
		{
			System.out.println("  " + cmd);
		}
	}
}
